﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Windows.Data.Json;

namespace HelloTipi.Api
{
    public class ApiTxt
    {
        private string username;
        private string password;
        private string my_sites_uri = "http://apitxt.hellotipi.com/account/my_sites.json";
        private string site_albums = "http://apitxt.hellotipi.com/photo/site_albums.json?site={0}";
        private string album_photos = "http://apitxt.hellotipi.com/photo/album_photos.json?site={0}&album_id={1}";
        private string favourite = "http://apitxt.hellotipi.com/photo/favourite.json";

        public ApiTxt(String UserName, String Password)
        {
            username = UserName;
            password = Password;
        }

        public async Task<Object> getSitesASync()
        {
            JsonArray Sites = await UriGetASync(my_sites_uri);

            var Items = from s in Sites
                        select new
                        {
                            name = s.GetObject()["name"].GetString(),
                            title = s.GetObject()["title"].GetString(),
                            role = s.GetObject()["role"].GetString(),
                            read_only = s.GetObject()["read_only"].GetNumber(),
                            nb_users = s.GetObject()["nb_users"].GetNumber(),
                            image_small = s.GetObject()["image_small"].GetString(),
                            image_small200 = s.GetObject()["image_small200"].GetString(),
                            image_medium = s.GetObject()["image_medium"].GetString(),
                            image_blur = s.GetObject()["image_blur"].GetString(),
                            new_public_msg = s.GetObject()["new_public_msg"].GetNumber()
                        };

            return Items;
        }

        public async Task<Object> getAlbumsASync(String Site)
        {
            JsonArray Albums = await UriGetASync(String.Format(site_albums, Site));

            //"image_small":null

            var Items = from a in Albums
                        select new
                        {
                            id = a.GetObject()["id"].GetString(),
                            name = a.GetObject()["name"].GetString(),
                            image_small = ((a.GetObject()["image_small"].ValueType == Windows.Data.Json.JsonValueType.Null) ? String.Empty : a.GetObject()["image_small"].GetString()),
                            default_album = a.GetObject()["default_album"].GetNumber()
                        };

            return Items;
        }

        public async Task<Object> getPhotosASync(String Site, String Album)
        {           
            JsonArray Photos = await UriGetASync(String.Format(album_photos, Site, Album));

            var Items = from p in Photos
                        select new
                        {
                            id = p.GetObject()["id"].GetNumber(),
                            url_small = p.GetObject()["url_small"].GetString(),
                            url_medium = p.GetObject()["url_medium"].GetString(),
                            url_large = p.GetObject()["url_large"].GetString(),
                            date = p.GetObject()["date"].GetString(),
                            exif_date = ((p.GetObject()["exif_date"].ValueType == Windows.Data.Json.JsonValueType.Null) ? String.Empty : p.GetObject()["exif_date"].GetString()),
                            user = p.GetObject()["user"].GetString()
                        };

            return Items;
        }

        public async Task<Object> getFavouriteASync()
        {
            JsonArray Favourites = await UriGetASync(favourite);

            var Items = from f in Favourites
                        select new
                        {
                            id = f.GetObject()["id"].GetNumber(),
                            url_small = f.GetObject()["url_small"].GetString(),
                            url_medium = f.GetObject()["url_medium"].GetString(),
                            url_large = f.GetObject()["url_large"].GetString()
                        };

            return Items;
        } 

        private async Task<JsonArray> UriGetASync(String uri)
        {
            HttpClientHandler handler = new HttpClientHandler
                {
                    Credentials = new NetworkCredential(username, password)
                };

            HttpClient client = new HttpClient(handler);

            HttpResponseMessage response = await client.GetAsync(uri);

            String JsonString = await response.Content.ReadAsStringAsync();

            //JsonObject();

            return JsonArray.Parse(JsonString);
        }
    }
}
